package org.example.service;

import org.example.pojo.MyUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

/**
 * @Auther: GongXl
 * @Date: 2021/3/9 13:44
 * @Description: 登录逻辑实现
 */
@Service
public class UserServiceImpl implements UserDetailsService {
    @Autowired
    PasswordEncoder passwordEncoder;
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
//        用username查询数据库
        if(!"admin".equals(username)){
            throw new UsernameNotFoundException("用户名或密码错误!");
        }
//        比较密码
        String password = passwordEncoder.encode("123456");
//        返回用户对象
//        return new User("admin",password, AuthorityUtils.commaSeparatedStringToAuthorityList("admin,normal,/insert,ROLE_abc,ROLE_role1"));
        return new MyUser("admin",password,AuthorityUtils.commaSeparatedStringToAuthorityList("admin,normal,/insert,ROLE_abc,ROLE_role1"));

    }
}